package qing.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import qing.dao.MoneyLogDao;
import qing.dao.PersonDao;
import qing.entity.Person;
import qing.entity.moneyLog;

@Service
public class moneyLogServiceImpl extends ServiceImpl<moneyLog> implements moneyLogService {


	@Autowired
	private MoneyLogDao imoneylog;
	
	@Autowired
	private PersonDao iperson;

	@Override
	public Float savemoney(int userid, Float money) {
		Person person=iperson.find(Person.class, userid);
		person.setAccount(person.getAccount()+money);
		imoneylog.create(new moneyLog("测试存钱", money, "junit test **"+new Date(), person));
		iperson.update(person);
		return person.getAccount();
	}

	@Override
	public Float drawmoney(int userid, Float money) {
		Person person=iperson.find(Person.class, userid);
	
		imoneylog.create(new moneyLog("测试取钱", -money, "junit test **"+new Date(), person));
		if (person.getAccount()<money) {
			throw new RuntimeException("账户余额不足，资产" + person.getAccount());
		}
		person.setAccount(person.getAccount()-money);
		iperson.update(person);
		return person.getAccount();
	}
	
	
	
}
